/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import Facade.CustomerFacadeLocal;
import entity.Customer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.persistence.NoResultException;
import util.JsfUtil;

/**
 *
 * @author Vojtisek
 */
public class LoginBean {

    @EJB
    private CustomerFacadeLocal customerFacade;
    private boolean loginOk;
    private String login;
    Integer customerId;
    Customer customer;
    private static final Logger logger = Logger.getLogger("LoginBean");

    public String validateLogin() {
        try {
            if (login != null) {
//                customer = customerFacade.find(login);
                customerId = customer.getId();
                loginOk = true;
                logger.log(Level.INFO, "Login OK: customerId: " + customerId + ", login: " + login);
                JsfUtil.addSuccessMessage("Login OK");
            }
        } catch (EJBException e) {
            loginOk = false;
            customer = null;
            customerId = null;
            logger.log(Level.WARNING, "Login Error: Login \"" + login + "\" does not exist.");
            JsfUtil.addErrorMessage("Login \"" + login + "\" does not exist.");
            return null;
        } catch (Exception ex) {
            loginOk = false;
            customer = null;
            customerId = null;
            JsfUtil.ensureAddErrorMessage(ex, "Persistance error");
            logger.log(Level.SEVERE, ex.getMessage(),ex);
            return null;
        }
        return null;
    }

    public String logOut() {
        loginOk = false;
        customer = null;
        customerId = null;
        return "index";
    }

    public boolean isLoginOk() {
        return loginOk;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String Login) {
        this.login = Login;
    }

    public Customer getCustomer() {
        return customer;
    }
}
